/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Reg_Member.java
 *
 * Created on Mar 22, 2010, 4:09:05 PM
 */

package Library.GUI;

import SourceCode.*;
import javax.swing.JOptionPane;
import javax.swing.table.*;

/**
 *
 * @author ssaad
 */
public class Reg_Member_Panel extends javax.swing.JPanel {

    /** Creates new form Reg_Member */
    DefaultTableModel defTableModel;

    public Reg_Member_Panel() {
        initComponents();
        defTableModel = new DefaultTableModel();
        this.jTable1.setModel(defTableModel);
        defTableModel.addColumn("MemberID");
        defTableModel.addColumn("MemberName");
        defTableModel.addColumn("PhoneNumber");
        defTableModel.addColumn("Address");
    }

    public void LoadMemberDataToTable() {
        //clearTable();
        
        int memberNumber = LibraryGeneral.S_Members.size();
        Object[] rowdata;
        for (int i = 0; i < memberNumber; i++) {
            int memberId = LibraryGeneral.S_Members.get(i).getMemberID();
            String memberName = LibraryGeneral.S_Members.get(i).getNAme();
            String memberPhone = LibraryGeneral.S_Members.get(i).getPhone();
            String memberAddress = LibraryGeneral.S_Members.get(i).getAddress();
            //rowdata[i] = {memberId, memberName, memberPhone, memberAddress};
            rowdata = new Object[]{memberId, memberName, memberPhone, memberAddress};
            defTableModel.addRow(rowdata);
        }
        //defTableModel.addRow();
        // this.jTable1
    }

    public void clearTable() {
        // clear the table
        int rowCount = defTableModel.getRowCount();
        if (rowCount > 0) {
            for (int i = 0; i < rowCount; i++) {
                defTableModel.removeRow(i);
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtNAme = new javax.swing.JTextField();
        txtID = new javax.swing.JTextField();
        txtAddress = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtPhone = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        btnMemberAdd = new javax.swing.JButton();
        btnMemberRemove = new javax.swing.JButton();
        btnMemberSearch = new javax.swing.JButton();

        jLabel1.setText("MemberID:");

        jLabel2.setText("Name:");

        jLabel3.setText("Address:");

        jLabel5.setText("Phone:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "MemberID", "Member Name", "Phone", "Address"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        btnMemberAdd.setText("Add");
        btnMemberAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMemberAddActionPerformed(evt);
            }
        });

        btnMemberRemove.setText("Remove");
        btnMemberRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMemberRemoveActionPerformed(evt);
            }
        });

        btnMemberSearch.setText("Search");
        btnMemberSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMemberSearchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtID)
                            .addComponent(txtNAme, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(btnMemberAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnMemberRemove)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 220, Short.MAX_VALUE)
                        .addComponent(btnMemberSearch)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNAme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)))
                    .addComponent(jLabel1))
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnMemberAdd)
                    .addComponent(btnMemberRemove)
                    .addComponent(btnMemberSearch))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(128, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnMemberSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMemberSearchActionPerformed
        // TODO add your handling code here:
        if(this.txtID.getText().equals("") || this.txtID.getText().equals(""))
        {
            //Show all members
            LoadMemberDataToTable();
        }

        Member member = LibraryGeneral.Map_Members.get(Integer.parseInt(this.txtID.getText()));
        int memberId = member.getMemberID();
        String memberName = member.getNAme();
        String memberPhone = member.getPhone();
        String memberAddress = member.getAddress();

        Object[] rowdata = new Object[]{memberId, memberName, memberPhone, memberAddress};

        clearTable();
        
        defTableModel.addRow(rowdata);



    }//GEN-LAST:event_btnMemberSearchActionPerformed

    private void btnMemberAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMemberAddActionPerformed
        // TODO add your handling code here:
        if(this.txtID.getText().equals("") || this.txtNAme.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null, "You can not add member, the ID and Name are empty.");
            return;
        }
        
        int memberID = Integer.parseInt(this.txtID.getText());
        String memberName = this.txtNAme.getText();
        String memberPhone = this.txtPhone.getText();
        String memberAddress = this.txtAddress.getText();
        Member newMember = new Member(memberID, memberName, memberPhone, memberAddress);
        LibraryGeneral.S_Members.add(newMember);

        LoadMemberDataToTable();
    }//GEN-LAST:event_btnMemberAddActionPerformed

    private void btnMemberRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMemberRemoveActionPerformed
        // TODO add your handling code here:
        int selectedRow = this.jTable1.getSelectedRow();
        Object o = this.jTable1.getValueAt(selectedRow, 0);
        int memberidNeedtoDelete = Integer.parseInt(o.toString());
        
    }//GEN-LAST:event_btnMemberRemoveActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnMemberAdd;
    private javax.swing.JButton btnMemberRemove;
    private javax.swing.JButton btnMemberSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtNAme;
    private javax.swing.JTextField txtPhone;
    // End of variables declaration//GEN-END:variables

}
